<?
$step_1_edit = translate('step_1_edit');
$step_2_set = translate('step_2_set');
$cancel_button = translate('cancel');
$next_step = translate('next_step');
$clear_form=translate('clear_form');
$planname=translate('planname');
$planperiod=translate('planperiod');
$months=translate('months');
$tlservice=translate('tlservice');
$tlquantity=translate('tlquantity');
$tlmo=translate('tlmo');
$tlbaseunit=translate('tlbaseunit');
$tlunitprice=translate('tlunitprice');
$totmonthprice=translate('totmonthprice');
$edit_plan_possible = translate('edit_plan_possible',array('planname' => $plname));

echo "<div align=center>
	<table border='0' cellspacing='0' cellpadding='5'>
		<tr>
			<td valign='middle' align='center'>
				<img src='images/ico_step_1.gif' border='0'>
			</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td valign='middle' align='center'>
				<img src='images/ico_current_step.gif' border='0'>
			</td>
			<td valign='middle' align='center'>$step_1_edit</td>
			<td valign='middle' align='center'>
				<img src='images/ico_next_step.gif' border='0'>
			</td>
			<td valign='middle' align='center'><font color='#FF6000'>$step_2_set</font></td>
		</tr>
	</table>
</div>
<br><br>";

if ($cl_count != 0) {
	echo "<script language='JavaScript'>
<!--
var myWin = '';


function openWin(url, name, properties) {
    if (myWin && myWin.open && !myWin.closed) {
	myWin.close();
    }
    myWin = window.open(url, name, properties);
    return false;
}
//-->
</script>";
	$params['sid'] = $sid;
	$params['action'] = "showPlanClientsDetails";
	$params['rid'] = $id;
	$params['pid'] = $prod_id;
	$detailsurl2 = "$baseURL/$webDIR/?".encode_params($params);

	unset($params);
	$params['sid'] = $sid;
	$params['action'] = "createPlan";
	$createurl = "$baseURL/$webDIR/?".encode_params($params);

	unset($params);
	$params['sid'] = $sid;
	$params['action'] = "moveClient2Plan";
	$moveurl = "$baseURL/$webDIR/?".encode_params($params);

	unset($params);
	$params['sid'] = $sid;
	$params['action'] = "managePlans";
	$params['sub'] = 'edit';
	$params['case'] = 1;
	$acturl = "$baseURL/$webDIR/?".encode_params($params);

	$msg = translate('edit_plan_impossible' , array('numberofclients' => $cl_count,
    							'planname' => $plname,
							'details' => $detailsurl2,
    							'create' => $createurl,
    							'move' => $moveurl));
	echo "<div align='left'>$msg</div>";
	echo "<form action='$acturl' method='POST'>
	<TABLE class='bTable' cellspacing=0>
		<tr>
			<td><input type=reset name=reset_form value='$cancel_button' onClick=\"history.back();\">&nbsp;
				<input type=submit name=submit value='$next_step'></td>
		</tr>
	</table>
	<input type='hidden' name='step2_edit' value='yes'>
	<input type='hidden' name='prod_id' value='$prod_id'>
	</form>";
	$hide_rest = 'yes';
} elseif ($prebuild == 'yes') {
	//------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']="services";
		$sql['table']='products';
		$sql['where']="product_id = '$prod_id' AND reseller_id = '$id'";
		$sql['options']='';
		$sql['return']='struct';
	//------------------------------------------------------
	if (!$result=sql_exec($sql,$dbh81,'assoc')) {
		$errorMsg = translate('int_error');
		include_once('errorMsg.html');
		exit();
	}
	$site_studio = $result[0]['services']['site_studio'];
	unset($params);
	$params['sid']=$sid;
	$params['action']='managePlans';
	$params['sub']='edit';
	$params['prebuild'] = 'yes';
	$params['case'] = 2;
	$acturl="$baseURL/$webDIR/?".encode_params($params);
	echo "<br><div align='left'>$edit_plan_possible</div><br>";
	echo "<form name='create_plan' action='$acturl' method='POST'>
	<TABLE class='bTable' cellspacing=0>
		<tr>
			<td>$planname :</td>
			<td><input type='text' name='plan_name' size='15'
				maxlength='10' value='$plname'></td>
		</tr>
		<tr>
			<td>".translate('site_studio')." :</td>
			<td><select name='site_studio'>
				<option	value='1'"; if ($site_studio == 1) echo "selected"; echo">On</option>
				<option value='0'"; if ($site_studio == 0) echo "selected"; echo">Off</option>
			</select></td>
		</tr>
		<tr>
			<td colspan='2' align='center'><input type='button' name='cancel' value='$cancel_button' onClick=\"history.back();\">&nbsp;
				<input type='reset' name='reset' value='$clear_form'>&nbsp;
				<input type='submit' name='submit' value='$next_step'>
				<input type='hidden' name='prod_id' value='$prod_id'>
				<input type='hidden' name='step2_edit' value='yes'></td>
		</tr>
	</table><br><br>";
	$hide_rest = 'yes';
} else {
	echo "<br><div align='left'>$edit_plan_possible</div><br>";
	//------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']='name, services';
		$sql['table']='products';
		$sql['where']="product_id='$prod_id'";
		$sql['options']='';
		$sql['return']='struct';
	//------------------------------------------------------
	if (!$result=sql_exec($sql,$dbh81,'assoc')) {
		$errorMsg = translate('int_error');
		include_once('errorMsg.html');
		exit();
	}
	$pl_name=$result[0]['name'];
	$pl_services=$result[0]['services'];
	echo "<br>";
	//------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']='currency, store_id';
		$sql['table']='stores';
		$sql['where']="reseller_id='$id'";
		$sql['options']='';
		$sql['return']='struct';
	//------------------------------------------------------
	if (!$result=sql_exec($sql,$dbh81,'assoc')) {
		$errorMsg = translate('int_error');
		include_once('errorMsg.html');
		exit();
	}
	$res_currency=$result[0]['currency'];
	$store_id=$result[0]['store_id'];
	//------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']="key_name, quantity, $res_currency as currency";
		$sql['table']='plan_elements AS a, plan_prices AS b';
		$sql['where']="a.id=b.id and a.key_name!='cgi'";
		$sql['options']='order by b.id,quantity';
		$sql['return']='struct';
	//------------------------------------------------------
	if (!$result=sql_exec($sql,$dbh81,'assoc')) {
		$errorMsg = translate('int_error');
		include_once('errorMsg.html');
		exit();
	}
	foreach ($result as $rt) {
		$jsarr[]='key_'.$rt['key_name'];
	}
	$jsarr=array_flip($jsarr);
	$jsarr=array_flip($jsarr);

	if (preg_match("/usd/i",$res_currency)) {
		$res_curr='$';
	} elseif (preg_match("/yen/i",$res_currency)) {
		$res_curr='&yen';
	}

	echo "<script language='JavaScript'>\n
	<!--\n";
	foreach ($jsarr as $gt) {
	echo "var ".$gt."_prices = new Array();\n";
	echo $gt."_prices['0']=0;\n";
	}
	#foreach ($jsarr as $gt) {
	#       $jsarr2[]="key_".$gt;
	#}
	$jsstr=implode("\",\"",$jsarr);
	echo "\n
	var names_selects = new Array (\"$jsstr\");";
	echo "\n";
	foreach ($result as $rt) {
		echo 'key_'.$rt[key_name]."_prices"."['".$rt[quantity]."']=".$rt[currency]."\n";
	}
	echo "\n";
	echo "
	function display_dec_string(input) {
		var ost = 0;
		var cch = 0;
		var result;
		cch = Math.floor(input);
		if( cch == 0 ) {
			ost = input;
		} else {
			ost = input % cch;
		}
		ost = Math.round(ost * 100);
		if ( ost < 10 ) ost = \"0\" + ost;
		result = cch + \".\" + ost;
		return result;
	}

	function calc_me (obj) {
		var total = 0;
		for (i=0; i<names_selects.length; i++) {
			var el = eval(\"obj.form.\" +names_selects[i]);
			var val = el.options[el.selectedIndex].value;
			get_price = eval(el.name + \"_prices['\" + val + \"']\");
			if (el.name == 'key_disk_space') {
				val = val/50;
			}
			get_unit_price = (val != 0) ? get_price/val : 0;
			document.getElementById(el.name + '_row').innerHTML = \"$res_curr\" +display_dec_string(get_price);
			document.getElementById(el.name + '_unit').innerHTML = \"$res_curr\" +display_dec_string(get_unit_price);
			tmp_v = el.options[el.selectedIndex].value;
			tmp_p = eval(names_selects[i] + \"_prices['\" + tmp_v + \"']\");
			if (el && el.options[el.selectedIndex].value) total+= parseFloat(tmp_p);
		}
		document.getElementById('total').innerHTML = \"$res_curr\" +display_dec_string(total);
	}
	function handleSupportSelect (obj) {
              if (obj.name == 'key_support') {
                   if (obj.options[obj.selectedIndex].value == '9999') {
                        if (!confirm('Are you sure you want to provide your own support?')) obj.options[0].selected = true;
                   }
              }
              calc_me(obj);
         }
	";
	echo "//-->
	</script>\n";
	unset($params);
	$params['sid']=$sid;
	$params['action']='managePlans';
	$params['sub']='edit';
	$params['case'] = 3;
	$acturl="$baseURL/$webDIR/?".encode_params($params);
	echo "<form name=create_plan action='$acturl' method='POST'>
	<TABLE class='bTable' cellspacing=0>
		<tr>
			<td>$planname :</td>
			<td><input type=text name=plan_name size=15 maxlength=10 value='$pl_name'></td>
			<td>$planperiod :</td>
			<td><select name=period>
				<option value=12>12 $months</option>
			</select></td>
		</tr>
	</table><br><br>";
	echo "<TABLE class='bTable' cellspacing=0>
		<tr>
			<th>$tlservice</th>
			<th>$tlquantity</th>
			<th nowrap>$tlmo</th>
			<td>&nbsp;</td>
			<th nowrap>$tlbaseunit</th>
			<th nowrap>$tlunitprice</th>
		</tr>";
	//------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']='*';
		$sql['table']='plan_elements';
		$sql['where']="key_name!='cgi'";
		$sql['options']='order by seq';
		$sql['return']='struct';
	//------------------------------------------------------
	if (!$result=sql_exec($sql,$dbh81,'assoc')) {
		$errorMsg = translate('int_error');
		include_once('errorMsg.html');
		exit();
	}
	foreach ($result as $res) {
		//------------------------------------------------------
			$sql['type']='SELECT';
			$sql['params']="quantity, $res_currency AS price";
			$sql['table']='plan_prices';
			$sql['where']="id='".$res['id']."'";
			$sql['options']='order by quantity';
			$sql['return']='struct';
		//------------------------------------------------------
		if (!$rlt=sql_exec($sql,$dbh81,'assoc')) {
			$errorMsg = translate('int_error');
			include_once('errorMsg.html');
				exit();
		}
		$service=translate($res['key_name']);
		echo "<tr>
			<td>$service</td>
			<td><select name='key_".$res['key_name']."' onChange=\"handleSupportSelect(this)\">";
		if ($res['id']!=1 && $res['id']!=2 && $res['id']!=3 && $res['id']!=4 && $res['id'] != 13 && $res['id'] != 10 && $res['id'] != 14) {
			echo "<option value=0>----</option>\n";
		}
		foreach ($rlt as $vl) {
			if ($res['key_name']=='disk_space') {
				$unit='50 megabytes';
				$unit1='MB';
			} elseif ($res['key_name']=='traffic') {
				$unit='1 gigabyte';
				$unit1='GB';
			} elseif ($res['key_name']=='domain') {
				if ($vl['quantity']==1) {
					$unit1='domain';
				} else {
					$unit1='domains';
				}
				$unit='domain';
			} elseif ($res['key_name']=='mailbox') {
				$unit='mail address';
				$unit1='addresses';
			} elseif ($res['key_name']=='sql') {
				$unit='database';
				if ($vl['quantity']==1) {
					$unit1='database';
				} else {
					$unit1='databases';
				}
			} elseif ($res['key_name']=='ip') {
				$unit='IP address';
				if ($vl['quantity']==1) {
					$unit1='address';
				} else {
					$unit1='addresses';
				}
			} elseif ($res['key_name']=='cgi' or  $res['key_name']=='site_studio') {
				$unit='installation';
				$unit1='unit';
			} elseif ($res['key_name']=='mail_list') {
				$unit='1 list';
				if ($vl['quantity']==1) {
					$unit1='list';
				} else {
					$unit1='lists';
				}
			} elseif ($res['key_name']=='ftp') {
				$unit='account';
				if ($vl['quantity']==1) {
					$unit1='account';
				} else {
					$unit1='accounts';
				}
			} elseif ($res['key_name']=='ssh') {
				$unit='shell';
			} elseif ($res['key_name']=='crontab') {
				$unit='cronjob';
				if ($vl['quantity']==1) {
				    $unit1 = 'cronjob';
				} else {
				    $unit1 = 'cronjobs';
				}
			} elseif ($res['key_name']=='subdomain') {
				$unit='subdomain';
				if ($vl['quantity']==1) {
				    $unit1 = 'subdomain';
				} else {
				    $unit1 = 'subdomains';
				}
			} else {
				$unit='1 unit';
				$unit1='unit';
			}

			if ($res['key_name'] == 'disk_space') {
				$tmp = $pl_services['disk_space']/1024;
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name'] == 'traffic') {
				$tmp = $pl_services['traffic']/1024000;
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
			}
			} elseif ($res['key_name'] == 'domain') {
				$tmp = $pl_services['domain'];
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name'] == 'mailbox') {
				$tmp = $pl_services['mailbox'];
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name'] == 'sql') {
				$tmp = $pl_services['sql'];
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name'] == 'ip') {
				$tmp = $pl_services['ip'];
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name'] == 'cgi') {
				$tmp = $pl_services['cgi'];
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name'] == 'mail_list') {
				$tmp = $pl_services['mail_list'];
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name'] == 'ftp') {
				$tmp = $pl_services['ftp'];
				if ($vl[quantity] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name'] == 'ssh') {
				$tmp = $pl_services['ssh'];
				if ($vl[quantity] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name'] == 'crontab') {
				$tmp = $pl_services['crontab'];
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name'] == 'subdomain') {
				$tmp = $pl_services['subdomain'];
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name']=='site_studio') {
				$tmp = $pl_services['site_studio'];
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			} elseif ($res['key_name']=='support') {
				$tmp = $pl_services['support'];
				if ($vl['quantity'] == $tmp) {
					$ch = 'selected';
				} else {
					$ch = '';
				}
			}
			$vl['price'] = sprintf("%01.2f",$vl['price']);
			if (count($rlt)==1) {
				echo "<option value='".$vl['quantity']."' $ch>Activate - ".$res_curr.$vl['price']."</option>\n";
			} elseif (count($rlt)!=1 && $res['key_name'] == 'support') {
				if ($vl['quantity'] == 1) {
					echo "<option value='$vl[quantity]' $ch>Activate - ".$res_curr."$vl[price]</option>\n";
				} else {
					echo "<option value='$vl[quantity]' $ch>Own Customer Support- ".$res_curr."$vl[price]</option>\n";
				}
			} else {
				echo "<option value='$vl[quantity]' $ch>$vl[quantity] $unit1 - ".$res_curr.$vl['price']."</option>\n";
			}
		}
		echo "</select></td>
			<td nowrap><div id='key_".$res['key_name']."_row' align=right>".$res_curr."0.00</div></td>
			<td nowrap>&nbsp;</td>
			<td nowrap>$unit</td>
			<td nowrap><div id='key_".$res['key_name']."_unit' align=right>".$res_curr."0.00</div></td>
		</tr>";
	}
	echo "<tr>
		<th colspan=2 align=right>$totmonthprice:</th>
		<th nowrap><div id=total align=right>".$res_curr."0.00</div></th>
		<td>&nbsp;</td>
		<th colspan=2 align=right>&nbsp;</th>
	</tr></table><br><br>";
	echo " <TABLE class='bTable' cellspacing=0>
		<tr>
			<td><input type='button' name='cancel' value='$cancel_button' onClick=\"history.back();\">&nbsp;
			<input type=reset name=reset_form value='$clear_form' onClick=\"this.form.reset();calc_me(this)\">&nbsp;
			<input type=submit name=submit value='$next_step'></td>
		</tr>
	</table>
	<input type='hidden' name='step2_edit' value='yes'>
	<input type='hidden' name='prod_id' value='$prod_id'>
	</form>\n
	<script language='JavaScript'>
		calc_me(document.create_plan.reset_form);\n
	</script>";
	echo "</div>";
	$hide_rest = 'yes';
}
?>
